<?php
/**
 * 基础Action
 */
class BaseAction extends Action {
    public function __construct()
	{
		$langSet = C('DEFAULT_LANG');
		
		// 读取项目公共语言包
		if (is_file(LANG_PATH . $langSet . '/common.php')) {
		    L(include LANG_PATH . $langSet . '/common.php');
		}
		// 载入菜单语言包
		if (is_file(LANG_PATH . $langSet . '/menu.php')) {
		    L(include LANG_PATH . $langSet . '/menu.php');
		}
		parent::__construct();
		
		//淘宝SDK引入
		include(APP_PATH . '/../taobao/Logger.php');
		include(APP_PATH . '/../taobao/TopClient.php');
		$this->taobao = new TopClient;
        $this->taobao->appkey = "12314997";
        $this->taobao->secretKey = "aa2511989ebb0739fb4bac1008a2f389";
		
		//后台菜单
		$top_menu = $this->admin_menu(0);
		$this->assign('top_menu', $top_menu);
		$curr_menuid = isset($_REQUEST['menuid']) && intval($_REQUEST['menuid']) ? intval($_REQUEST['menuid']) : '';
		$this->assign('curr_menuid', $curr_menuid);
	}
	
	/**
	 * 菜单
	 */
	protected function admin_menu($pid, $with_self=false)
	{
	    $pid = intval($pid);
	    $where_ext = '';
	    if ($with_self) {
	        $where_ext = " OR id='$pid'";
	    }
	    $result = M('menu')->where("pid='$pid'".$where_ext." AND enabled='1'")->order('sort_order')->select();
	    $menus = array();
	    foreach ($result as $val) {
	        $val['title'] = L($val['name']);
	        $menus[] = $val;
	    }
	    return $menus;
	}
	
	protected function error($message, $url_forward='',$ms = 3, $dialog=false, $ajax=false, $returnjs = '')
	{
	    $this->jumpUrl = $url_forward;
	    $this->waitSecond = $ms;
	    $this->assign('dialog',$dialog);
	    $this->assign('returnjs',$returnjs);
	    parent::error($message, $ajax);
	}
	
	protected function success($message, $url_forward='',$ms = 3, $dialog=false, $ajax=false, $returnjs = '')
	{
	    $this->jumpUrl = $url_forward;
	    $this->waitSecond = $ms;
	    $this->assign('dialog',$dialog);
	    $this->assign('returnjs',$returnjs);
	    parent::success($message, $ajax);
	}
	
	public function simplexml_obj2array($obj)
	{
        if ($obj instanceof SimpleXMLElement) {
            $obj = (array)$obj;
        }

        if (is_array($obj)) {
            $result = $keys = array();
            foreach ( $obj as $key=>$value) {
                isset($keys[$key]) ? ($keys[$key] += 1) : ($keys[$key] = 1);

                if ( $keys[$key] == 1 ) {
                    $result[$key] = self::simplexml_obj2array($value);
                } elseif ( $keys[$key] == 2 ) {
                    $result[$key] = array($result[$key], self::simplexml_obj2array($value));
                } else if( $keys[$key] > 2 ) {
                    $result[$key][] = self::simplexml_obj2array($value);
                }
            }
            return $result;
        } else {
            return $obj;
        }
    }
    
    public function saddslashes($value)
    {
        if (empty($value)) {
            return $value;
        } else {
            return is_array($value) ? array_map(array('BaseAction','saddslashes'), $value) : addslashes($value);
        }
    }
}
?>
